package com.cheng.student.controller.filter;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//拦截未登录的客户端并使其重定向到登陆页面
@WebFilter(urlPatterns = {"/teacQuery.html","/stuQuery.html"})
public class Power0Filter
        implements Filter
{
    public void init(FilterConfig config)
            throws ServletException
    {
    }

    public void destroy()
    {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws ServletException, IOException
    {
        if (((HttpServletRequest)request).getSession().getAttribute("teacher") == null)
        {
            ((HttpServletResponse)response).sendRedirect("/index.html");
            return;
        }
        else
        {
            ((HttpServletRequest)request).getSession().setMaxInactiveInterval(300);
        }
        chain.doFilter(request, response);
    }
}
